<template>
  <div>
    <el-form class="query-table-form" @submit.native.prevent :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="项目名称查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          placeholder="负责人查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          placeholder="地址查询"
          auto-top
          clearable
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <el-tooltip effect="light" :content="scope.row.address">
          <div class="text-truncate">{{ scope.row.address }}</div>
        </el-tooltip>
      </template>

      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.res_a_name }}</div>
              <div>B角:{{ scope.row.res_b_name }}</div>
            </div>
          </div>
        </el-popover>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <attach-file :customerRelatedId="attachId" :visible.sync="attachVisible"></attach-file>

    <el-dialog
      :visible.sync="contractVisible"
      :close-on-click-modal="false"
      :before-close="beforeClose"
      append-to-body
      top="30px"
      width="80%"
    >
      <el-tabs v-model="activeName">
        <el-tab-pane label="签约登记" name="first">
          <contract-registr
            :query-id="contractRelated.customer_related_id"
            view-mode
          ></contract-registr>
        </el-tab-pane>
        <el-tab-pane label="放款审批">
          <loan-approve-view
            :view-mode="true"
            :queryId="contractRelated.customer_related_id"
          ></loan-approve-view>
        </el-tab-pane>
        <el-tab-pane label="放款通知书" name="notice" v-if="+contractRelated.is_history !== 1">
          <loan-notice :query-id="contractRelated.customer_related_id"></loan-notice>
        </el-tab-pane>
        <el-tab-pane label="合同审批">
          <contract-approve
            :query-id="contractRelated.customer_related_id"
            view-mode
          ></contract-approve>
        </el-tab-pane>
        <el-tab-pane label="合同附件" name="contract">
          <contract-guard-view
            :query-id="contractRelated.customer_related_id"
          ></contract-guard-view>
        </el-tab-pane>
        <el-tab-pane label="附件">
          <attach-view
            :queryId="contractRelated.customer_related_id"
            :visible="contractVisible"
          ></attach-view>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>

    <register-edit-reply-dialog
      :visible.sync="replyVisible"
      @success="handleSuccess"
      :relatedData="replyRelated"
    ></register-edit-reply-dialog>
  </div>
</template>

<script>
import SelectAddress from '@/components/select/SelectAddress'
import AttachFile from '@/credit/views/city/program/Attach'
import RegisterEditReplyDialog from '@/credit/views/county/mission/RegisterEditReplyDialog'

import taskTableMixin from '@/credit/mixin/self/taskTable'

import { enumGuatype } from '@/credit/plugins/enumTown'
import ContractRegistr from '@/credit/views/city/self/components/contractRegistr/ContractRegistr'
import ContractApprove from '@/credit/views/city/self/components/contractApprove/Main'
import ContractGuardView from '@/credit/views/county/unionProvince/components/ContractGuardView'
import AttachView from '@/credit/components/AttachView'
import LoanNotice from '@/credit/views/county/unionProvince/components/LoanNotice'
import LoanApproveView from '@/credit/views/city/self/components/loanApprove/Main'
export const registerTableColumns = [
  {
    attrs: { label: '主体名称', prop: 'customer_name', minWidth: '120px' },
    process: (v, row) => ({
      attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
    }),
  },
  {
    attrs: { label: '业务类型', prop: 'gua_type', align: 'center' },
    process: v => enumGuatype[v] || '-',
  },
  { attrs: { label: '地址', prop: 'address' }, slot: 'address' },
  { attrs: { label: '负责人', prop: 'corp_name', align: 'center' } },
  { attrs: { label: '联系电话', prop: 'mobile_no', align: 'center' } },
  { attrs: { label: '放款金额(万元)', prop: 'reply_funds', align: 'center' } },
]

export default {
  components: {
    SelectAddress,
    AttachFile,
    RegisterEditReplyDialog,
    ContractRegistr,
    ContractApprove,
    ContractGuardView,
    AttachView,
    LoanNotice,
    LoanApproveView,
  },
  mixins: [taskTableMixin],
  props: {},
  data() {
    return {
      params: {
        index: 1,
        size: 10,
        customer_name: '',
        corp_name: '',
        address_code: '',
        'gua_type.eq': 2,
        'contract_approval_status.eq': 3,
        'loan_approval_status.eq': 2,
      },
      order: {
        load_date: 'DESC',
      },
      useRegisterTable: true,
      tableColumns: [
        ...registerTableColumns,
        // { attrs: { label: '放款时间', prop: 'noticeTime', align: 'center' } },
        { attrs: { label: '合作银行', prop: 'bank_name', align: 'center' } },
        { attrs: { label: '项目经理', prop: 'resName', width: 100 }, slot: 'name' },
        {
          attrs: { label: '操作', align: 'center', minWidth: 120 },
          operations: [
            {
              content: '放款登记',
              icon: 'fa fa-gavel',
              event: this.handleEdit,
              // 为与任务保持一致，AB角登记
              ifRender: row => [row.res_a, row.res_b].includes(this.userId),
            },
            {
              content: '附件',
              icon: 'fa fa-clipboard',
              event: this.handleAttach,
            },
            {
              content: '查看',
              icon: 'fa fa-search',
              event: this.hanleView,
            },
          ],
        },
      ],

      // 放款登记
      replyRelated: {},
      replyVisible: false,

      // 担保合同
      contractRelated: {},
      contractVisible: false,

      // 附件
      attachId: '',
      attachVisible: false,

      activeName: 'first',
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getData()
  },
  mounted() {},
  methods: {
    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type] || '-'
      item.noticeTime = this.$utils.dateFormat(item.load_date, 'yyyy-MM-dd')
      item.resName = `A角: ${item.res_a_name} B角: ${item.res_b_name}`
      item.submitType = +item.submit_type
      item.isGovSubmit = item.submitType === 1

      return item
    },
    handleAttach(row) {
      this.attachId = row.customer_related_id
      this.attachVisible = true
    },
    handleEdit(row = {}) {
      this.replyRelated = { ...row }
      this.replyVisible = true
    },
    hanleView(row) {
      this.activeName = 'first'
      this.contractRelated = row
      this.contractVisible = true
    },
    beforeClose() {
      this.contractVisible = false
    },
    handleSuccess() {
      this.$store.dispatch('getTaskCount')
      this.getData()
    },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
